﻿using System;
using Microsoft.Xna.Framework;
using Imaginecup.util.resourceMan;
using Imaginecup.util.bladeEffect;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;

namespace Imaginecup.util.input
{
    public class TouchTrailLayer
    {
        private static TouchTrailLayer m_TouchTrailLayer = null;
        private TouchTrailLayer()
        {
#if WINDOWS_PHONE
            m_BladeDic = new Dictionary<int, CBlade>();
            m_BladeRemoveID = new List<int>();

#endif
#if WINDOWS || XBOX

            m_BladeList = new List<CBlade>();
#endif
        }

        public static TouchTrailLayer GetTouchTrailLayer()
        {
            if (m_TouchTrailLayer == null)
            {
                m_TouchTrailLayer = new TouchTrailLayer();
            }

            return m_TouchTrailLayer;
        }

#if WINDOWS_PHONE
        private Dictionary<int, CBlade> m_BladeDic;
        private List<int> m_BladeRemoveID;
   

        public void Release()
        {
            foreach (KeyValuePair<int, CBlade> pair in m_BladeDic)
            {
                pair.Value.Release();
            }

            m_BladeDic.Clear();
        }

        public bool IsContain(TouchLocation touch)
        {
            if (!m_BladeDic.ContainsKey(touch.Id)) return false;

            return true;
        }

        public Vector2 GetTopPath( TouchLocation touch )
        {
            CBlade blade = m_BladeDic[touch.Id];
            return blade.GetTopPath();
        }

        public void SetTexture( TouchLocation touch , Texture2D texture)
        {
            if( m_BladeDic.ContainsKey(touch.Id))
            {
                CBlade blade = m_BladeDic[touch.Id];
                blade.SetTexture(texture);
            }
        }

        public void TouchBegan( TouchLocation touch , Texture2D texture , int  width)
        {
            CBlade blade = CBlade.BladeWithMaximumPoint(50, width);
            blade.SetTexture(texture);//ResourceMan.GetResourceMan().GetTexture("streak" + RandomUtil.rand.Next(1, 5)));
            blade.Push(touch.Position);

            m_BladeDic.Add(touch.Id, blade);
        }

        public void TouchMove(TouchLocation touch)
        {
            if (!m_BladeDic.ContainsKey(touch.Id)) return;

            CBlade blade = m_BladeDic[touch.Id];
            if (blade.GetEndState()) return;

            blade.Push(touch.Position);
        }

        public void TouchEnd(TouchLocation touch)
        {
            if (!m_BladeDic.ContainsKey(touch.Id)) return;
                
            // 눌르고 있는 상태에서 메뉴 키(일시정지) 눌려졌을때.. 강제로 End시킨다(Malaria의 AddEvent 루프 및 TouchAllEnd함수 참고)
            CBlade blade = m_BladeDic[touch.Id];
            if (blade.GetEndState()) return;

            blade.SetDecreaseState();
            
        }

        public void Update()
        {
            foreach (KeyValuePair<int, CBlade> pair in m_BladeDic)
            {
                pair.Value.Update();

                if( pair.Value.GetEndState() )
                {
                    m_BladeRemoveID.Add( pair.Key );
                }
            }

            if( m_BladeRemoveID.Count > 0)
                RemoveBlade();
        }

        private void RemoveBlade()
        {
            foreach( int key in m_BladeRemoveID )
            {
                m_BladeDic.Remove(key);
            }

            m_BladeRemoveID.Clear();
        }

        public void Draw()
        {
            foreach (KeyValuePair<int, CBlade> pair in m_BladeDic)
            {
                pair.Value.Draw();
            }
        }
        public void TouchAllEnd()
        {
            foreach (KeyValuePair<int, CBlade> pair in m_BladeDic)
            {
                pair.Value.SetDecreaseState();
            }
        }

    }
#endif

#if WINDOWS || XBOX
        public static Random rand = new Random();
        private List<CBlade> m_BladeList;
        private bool m_bIsEffectGenerator = false;

        
        public void TouchBegan(int X, int Y , Texture2D texture , int  width)
        {
            m_BladeList.Add( CBlade.BladeWithMaximumPoint(50 , width) );

            m_BladeList[m_BladeList.Count - 1].SetTexture(texture);//ResourceMan.GetResourceMan().GetTexture("streak" + rand.Next(1, 5)));

            m_BladeList[m_BladeList.Count - 1].Push(new Vector2(X, Y));
        }

        public void TouchMove(int X, int Y)
        {
            if (m_BladeList.Count == 0)
            {
                m_bIsEffectGenerator = false;
                return;
            }
            else if( m_BladeList[m_BladeList.Count - 1].GetEndState() ) return ;           

            
            m_BladeList[m_BladeList.Count - 1].Push(new Vector2(X, Y));
            m_bIsEffectGenerator = true;
        }

        public void TouchEnd()
        {
            if (m_BladeList.Count == 0) return;
            if( m_BladeList[m_BladeList.Count - 1].GetEndState() ) return ;
            else 
                m_BladeList[m_BladeList.Count - 1].SetDecreaseState();
            m_bIsEffectGenerator = false;
        }

        public bool IsGenerator()
        {
            if( m_BladeList.Count > 0)
                return m_bIsEffectGenerator ;
            return false;
        }

        public void Draw()
        {
            foreach (CBlade blade in m_BladeList)
            {
                blade.Draw();
            }
        }

        public void Update()
        {
            for (int i = m_BladeList.Count - 1; i >= 0; i--)
            {
                CBlade blade = m_BladeList[i];

                blade.Update();

                if (blade.GetEndState())
                {
                    m_BladeList.Remove(blade);
                }
            }
        }

        public Vector2 GetTopPath()
        {
 
            CBlade blade = m_BladeList[m_BladeList.Count - 1];
            return blade.GetTopPath();
        }

        public void TouchAllEnd()
        {
            if (m_BladeList.Count > 0) 
                m_BladeList[m_BladeList.Count - 1].SetDecreaseState();
            m_bIsEffectGenerator = false;
        }

        public void Release()
        {
            foreach( CBlade blade in m_BladeList )
            {
                blade.Release();
            }

            m_BladeList.Clear();
        }
    }

#endif
}
